package com.platform.batch.base.util;

import com.google.common.collect.Maps;
import lombok.Data;

import java.io.Serializable;
import java.util.Map;

@Data
public class XportConfig implements Serializable {
    private static final Integer DEFAULT_SHEET_NUM = 0;
    private static final Integer DEFAULT_TITLE_ROW_NUM = 1;
    private static final Integer DEFAULT_FIRST_ROW_NUM = 2;
    private static final Integer DEFAULT_FIRST_COL_NUM = 0;
    public String sheetName;
    public Integer sheetNum;
    public Integer titleRowNum;
    public Integer firstRowNum;
    public Integer firstColNum;
    public String templatePath;
    public Integer templateMinRows;
    public Integer templateRowNum;
    public final Map<String, Object> fixedData;
    public ColumnConfig[] columns;
    public final Map<String, Object> context;

    public XportConfig() {
        this.sheetNum = DEFAULT_SHEET_NUM;
        this.titleRowNum = DEFAULT_TITLE_ROW_NUM;
        this.firstRowNum = DEFAULT_FIRST_ROW_NUM;
        this.firstColNum = DEFAULT_FIRST_COL_NUM;
        this.fixedData = Maps.newHashMap();
        this.context = Maps.newHashMap();
    }

    public void extendColumns(int colCount) {
        int oldSize = this.getColumnsCount();
        if (this.columns == null || colCount > this.columns.length) {
            ColumnConfig[] cols = new ColumnConfig[colCount];
            for (int i = 0; i < colCount; ++i) {
                if (i < oldSize) {
                    assert this.columns != null;
                    cols[i] = this.columns[i];
                } else {
                    cols[i] = new ColumnConfig();
                }
            }
            this.columns = cols;
        }
    }

    public int getColumnsCount() {
        return this.columns == null ? 0 : this.columns.length;
    }
}
